package com.zhsource.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhsource.system.domain.Role;
import com.zhsource.system.mapper.RoleMapper;
import com.zhsource.system.query.RoleQuery;
import com.zhsource.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>@ClassName：RoleServiceImpl
 * <p>@Date 2023/10/17  13:01
 *
 * @Author zzl
 **/
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> list() {
        return roleMapper.list();
    }

    @Override
    public Role selectById(Long id) {
        return roleMapper.selectById(id);
    }

    //    @Override
//    public PageResult<List<Role>> page(RoleQuery roleQuery) {
////        // 1.查询总条数
//        Integer total = roleMapper.total(roleQuery);
//        // 2.查询当前页数据
//        List<Role> page = roleMapper.page(roleQuery);
//
//        return  new PageResult<>(total,page);
//
//    }
    @Override
    public PageInfo<Role> page(RoleQuery roleQuery) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        PageHelper.startPage(roleQuery.getCurrentPage(), roleQuery.getPageSize());

        // 2.执行SQL语句带条件查询，SQL语句不需要进行分页
        List<Role> page = roleMapper.page(roleQuery);
        // 3.将查询结果转换为PageInfo对象，PageInfo对象中有前端分页所需要的各种参数
        PageInfo<Role> pageInfo = new PageInfo<>(page);
        // 4.响应总条数和分页信息
        return pageInfo;

    }

    @Override
    public void update(Role role) {
        // 修改角色信息
        roleMapper.update(role);

        // 2.删除角色和角色菜单中间表此角色所对应的所有数据

        List<Long> permissionIds = role.getPermissionIds();
        List<Long> menuIds = role.getMenuIds();
        roleMapper.deleteRoleAndPermissionId(role.getId());
        roleMapper.deleteRoleAndMenuIds(role.getId());

        // 3.批量新增角色权限和角色菜单中间表数据，如果集合为空，那么不在批量新增

        if (permissionIds != null && permissionIds.size() > 0) {
            roleMapper.batchInsertRoleAndPermission(role.getId(), permissionIds);
        }
        if (menuIds != null && menuIds.size() > 0) {
            roleMapper.batchInsertRoleAndMenu(role.getId(), menuIds);
        }

    }

    @Override
    public void insert(Role role) {
        roleMapper.insert(role);
        // 获取到角色的新增主键
        Long id = role.getId();
        List<Long> menuIds = role.getMenuIds();
        List<Long> permissionIds = role.getPermissionIds();
        // 批量新增角色与菜单和权限的中间表数据
        if (permissionIds != null && permissionIds.size() > 0) {
            roleMapper.batchInsertRoleAndPermission(id, permissionIds);
        }
        if (menuIds != null && menuIds.size()> 0) {
            roleMapper.batchInsertRoleAndMenu(id, menuIds);

        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByRoleId(id);
        roleMapper.deleteRoleAndPermissionId(id);
        roleMapper.delete(id);
    }

    @Override
    public void batchDel(List<Long> ids) {
        roleMapper.batchDeleteByRoleIds(ids);
        roleMapper.batchDeleteByEmployeeIds(ids);
        roleMapper.batchDel(ids);
    }

}
